// pages/index/select_community/select_community.js
import {
  Toast
} from 'wuss-weapp';
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isAuthorization: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let than = this
    // 查看是否授权
    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称
          wx.getUserInfo({
            success: function (res) {
              app.globalData.wxUserInfo = res.userInfo
              than.login()
              than.setData({
                isAuthorization: true
              })
            }
          })
        } else {
          than.setData({
            isAuthorization: false
          })
        }
      }
    })
    if (true) {
      if (app.globalData.appType == 1) {
        than.onItemXL()
      } else if (app.globalData.appType == 2) {
        than.onItemHL()
      }else if (app.globalData.appType == 3) {
        than.onItemTL()
      }else if (app.globalData.appType == 4) {
        than.onItemLXL()
      }else if (app.globalData.appType == 5) {
        than.onItemHT()
      } else {

      }
      return;
    }
  },

  login: function () {
    let than = this
    wx.login({
      success: res => {
        const accountInfo = wx.getAccountInfoSync();
        app.wxGetRequest("/wechart/mine/authorize", {
          js_code: res.code,
          applet_id: accountInfo.miniProgram.appId,
          name: app.globalData.wxUserInfo["nickName"],
          head_img: app.globalData.wxUserInfo["avatarUrl"]
        }).then(result => {
          app.globalData.userInfo = result.data.data;
          wx.setStorageSync('user_id', app.globalData.userInfo["user_id"]);
          wx.setStorageSync('people_id', app.globalData.userInfo["people_id"]);
          wx.setStorageSync('name', app.globalData.userInfo["name"]);
          if (!app.globalData.userInfo["people_id"]) {
            if (app.globalData.appType == 1) {
              than.onItemXL()
            } else if (app.globalData.appType == 2) {
              than.onItemHL()
            }else if (app.globalData.appType == 3) {
              than.onItemTL()
            }else if (app.globalData.appType == 4) {
              than.onItemLXL()
            }else if (app.globalData.appType == 5) {
              than.onItemHT()
            } else {

            }
            return;
          }
          app.isBindCommunity().then(result => {
            if (app.globalData.appType == 1) {
              than.onItemXL()
            } else if (app.globalData.appType == 2) {
              than.onItemHL()
            }else if (app.globalData.appType == 3) {
              than.onItemTL()
            }else if (app.globalData.appType == 4) {
              than.onItemLXL()
            }else if (app.globalData.appType == 5) {
              than.onItemHT()
            } else {

            }
          });
        });
      }
    });

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },


  /**
   * 棠湖社区
   */
  onItemXL: function () {
    if (true) {
      wx.setStorage({
        key: "community",
        data: 1
      })
      app.globalData.appTitle = '棠湖社区'
      app.globalData.unitCode = '86.001.001.001.009.003.002'
      app.globalData.parentAreaCode = '86.51.01.16.03.02'
      wx.switchTab({
        url: '/pages/index/index',
      })
      app.wxGetRequest('/applet/associateArea', {
        user_id: app.globalData.userInfo["user_id"] == 'test' ? '1' : app.globalData.userInfo["user_id"],
        area_code: app.globalData.parentAreaCode
      })
    } else {
      wx.navigateTo({
        url: '/pages/mine/login/login',
      })
    }

  },
  /**
   * 紫东阁社区
   */
  onItemHL: function () {
    if (true) {
      wx.setStorage({
        key: "community",
        data: 2
      })
      app.globalData.appTitle = '紫东阁社区'
      app.globalData.unitCode = '86.001.001.001.009.003.003'
      app.globalData.parentAreaCode = '86.51.01.16.03.03'
      wx.switchTab({
        url: '/pages/index/index',
      })
      app.wxGetRequest('/applet/associateArea', {
        user_id: app.globalData.userInfo["user_id"] == 'test' ? '1' : app.globalData.userInfo["user_id"],
        area_code: app.globalData.parentAreaCode
      })
    } else {
      wx.navigateTo({
        url: '/pages/mine/login/login',
      })
    }
  },
  /**
   * 五洞桥社区
   */
  onItemTL: function () {
    if (true) {
      wx.setStorage({
        key: "community",
        data: 3
      })
      app.globalData.appTitle = '五洞桥社区'
      app.globalData.unitCode = '86.001.001.001.009.003.004'
      app.globalData.parentAreaCode = '86.51.01.16.03.04'
      wx.switchTab({
        url: '/pages/index/index',
      })
      app.wxGetRequest('/applet/associateArea', {
        user_id: app.globalData.userInfo["user_id"] == 'test' ? '1' : app.globalData.userInfo["user_id"],
        area_code: app.globalData.parentAreaCode
      })
    } else {
      wx.navigateTo({
        url: '/pages/mine/login/login',
      })
    }
  },
  /**
   * 城塔社区
   */
  onItemLXL: function () {
    if (true) {
      wx.setStorage({
        key: "community",
        data: 4
      })
      app.globalData.appTitle = '城塔社区'
      app.globalData.unitCode = '86.001.001.001.009.003.005'
      app.globalData.parentAreaCode = '86.51.01.16.03.05'
      wx.switchTab({
        url: '/pages/index/index',
      })
      app.wxGetRequest('/applet/associateArea', {
        user_id: app.globalData.userInfo["user_id"] == 'test' ? '1' : app.globalData.userInfo["user_id"],
        area_code: app.globalData.parentAreaCode
      })
    } else {
      wx.navigateTo({
        url: '/pages/mine/login/login',
      })
    }
  },
  /**
   * 丰乐社区
   */
  onItemHT: function () {
    if (true) {
      wx.setStorage({
        key: "community",
        data: 5
      })
      app.globalData.appTitle = '丰乐社区'
      app.globalData.unitCode = '86.001.001.001.009.003.006'
      app.globalData.parentAreaCode = '86.51.01.16.03.06'
      wx.switchTab({
        url: '/pages/index/index',
      })
      app.wxGetRequest('/applet/associateArea', {
        user_id:app.globalData.userInfo["user_id"] == 'test' ? '1' : app.globalData.userInfo["user_id"],
        area_code: app.globalData.parentAreaCode
      })
    } else {
      wx.navigateTo({
        url: '/pages/mine/login/login',
      })
    }
  }
})